<?php
mysql_connect("localhost","root","") or die("Cannt connect to db");
mysql_select_db("gs3_hotelreservation") or die("Cannt find db name");

if(isset($_POST['functions'])){
    $title = @$_POST['title'];
    $start = @$_POST['start'];
    $end = @$_POST['end'];
    $allday = @$_POST['allDay'];
    $room_id = @$_POST['room_id'];
    $description = @$_POST['description'];
    $checkInStatus = @$_POST['checkInStatus'];
    $startDate = @$_POST['startDate'];
    $endDate = @$_POST['endDate'];
    if($_POST['functions']=="save"){
        mysql_query("INSERT INTO try2 (`id`, `title`, `start`, `end`,`allday`,`room_id`,`description`)
                    VALUES (NULL, '$title', '$start',
                    '$end','$allday','$room_id','$description')");
    }elseif($_POST['functions']=="update"){
        $id = $_POST['id'];
        mysql_query("Update try2 set start='$start', end='$end',allday='$allday' where id = $id");
    }elseif($_POST['functions']=="updateTitle"){
        $id = $_POST['id'];
        $id = substr($_POST['id'],5,strlen($_POST['id']));
        mysql_query("Update try2 set title='$title', description = '$description',checkIn = '$checkInStatus',start='$startDate',end = '$endDate',allday='$allday' where id = $id");
    }elseif($_POST['functions']=="delete"){
        $id = $_POST['id'];
        $id = substr($_POST['id'],6,strlen($_POST['id']));
        mysql_query("delete from try2 where id = $id");
    }
    ?>
    <script>
        window.location.reload(true);
    </script>
    <?php
}elseif(isset($_POST['description'])){
    $id = $_POST['description'];
    $query = mysql_query("select * from try2 where id = '$id'");
    $row = mysql_fetch_array($query);
    ?>
    <span style="float: right; color: red; cursor: pointer" id="closeTool"><img src="close.png" /></span>
    <table width=150>
        <tr>
            <td>Title</td>
            <td><?php echo $row['title'] ?></td>
        </tr>
        <tr>
            <td>Description:</td>
            <td><?php echo $row['description'] ?></td>
        </tr>
        <tr>
            <td>Status:</td>
            <td>
                <?php
                if($row['checkIn']==0){
                    echo "Reserve";
                }elseif($row['checkIn']==1){
                    echo "Check In";
                }else{
                    echo "Check Out";
                }
                ?>
                
            </td>
        </tr>
        <tr>
            <td>Date Start:</td>
            <td>
            <?php
                $dateS = $row['start'];
                echo  date("Y-m-d H:i:s", strtotime($dateS));
            ?>
            </td>
        </tr>
        <tr>
            <td>Date End:</td>
            <td>
            <?php
                $dateE = $row['end'];
                echo  date("Y-m-d H:i:s", strtotime($dateE));
            ?>
            </td>
        </tr>
    </table>
    <br>
        
    <a class='editbut' id='edbut<?php echo $id ?>' border='0' style='cursor: pointer;float: left;padding-right:5px; padding-top:2px;color:blue;'>Edit</a><a class='delbut' id='delbut<?php echo $id ?>' border='0' style='color:blue;cursor: pointer;padding-right:5px; float: right; padding-top:2px;'>Delete</a>
    <?php
}elseif(isset($_POST['getVal'])){
    $id = $_POST['id'];
    $id = substr($_POST['id'],5,strlen($_POST['id']));
    $query = mysql_query("select * from try2 where id = '$id'");
    $row = mysql_fetch_array($query);
    if($row['checkIn']==1){
        $status = "Check Out";
        $valStat = "2";
    }else{
        $status = "Check In";
        $valStat = "1";
    }
    $startD = $row['start'];
    $endD = $row['end'];
    $allD = $row['allday'];
    ?>
    <script>
        $("#title").val("<?php echo $row['title']; ?>");
        $(".desc").val("<?php echo $row['description']; ?>");
        <?php
        if($row['checkIn']!=2){
            ?>
            $("#checkIn").html("<span id='notLoadInAdd'>"+
                               "<label>Status:</label>"+
                               "<span style='float:left'><input type='checkbox' id='stat_id' rel='<?php echo $row['checkIn'] ?>' value='<?php echo $valStat ?>' name='stat' />"+
                                "<?php echo $status ?>"+
                                "</span><br />"+
                                "<label>Status:</label>"+
                                "<span style='float:left'>"+
                                "<input type='checkbox' id='allday'<?php if($allD=='true'){echo "checked='checked'";} ?> />"+
                                    "All day"+
                                "</span><br /><br /><br />"+
                               "</span>"+
                               "<span id='dates'>"+
                                "<label>Start Date:</label>"+
                                "<input type='text' value='<?php echo $startD; ?>' class='ssDate' readonly='readonly' <?php if($allD=='true'){echo "id='startDate'";}else{echo "id='startDates'";} ?> /><br /><br />"+
                                "<label>End Date:</label>"+
                                "<input type='text' value='<?php echo $endD; ?>' class='eeDate' readonly='readonly' <?php if($allD=='true'){echo "id='endDate'";}else{echo "id='endDates'";} ?>/>"+
                               "</span>");
            <?php
        }
        ?>
        
    </script>
    <!--
    id=startDate and endDate == allday;
    id=startDates and endDates == not allday;
    -->
    <script type="text/javascript">
        $(document).ready(function(){
            $('#startDate').change(function(){
                $("#endDate").val(""); 
            })
            $("#allday").click(function(){
                if($(this).attr("checked")){
                    startVal = $("#startDates").val();
                    endVal = $("#endDates").val();
                    $("#dates").html("<label>Start Date:</label><input type='text' class='ssDate' id='startDate' value='"+startVal+"' readonly='readonly'/><br /><br /><label>End Date:</label><input type='text' class='eeDate' value='"+endVal+"' id='endDate' />")
                    MakeStartEndPicker1("#startDate", "#endDate");
                }else{
                    startVal = $("#startDate").val();
                    endVal = $("#endDate").val();
                    $("#dates").html("<label>Start Date:</label><input type='text' class='ssDate' id='startDates' value='"+startVal+"' readonly='readonly'/><br /><br /><label>End Date:</label><input type='text' class='eeDate' value='"+endVal+"' id='endDates' />")
                    var startDateTextBox = $('#startDates');
                    var endDateTextBox = $('#endDates');
                    
                    startDateTextBox.datetimepicker({
                        timeFormat: 'hh:mm tt',
                        onClose: function(dateText, inst) {
                                if (endDateTextBox.val() != '') {
                                        var testStartDate = startDateTextBox.datetimepicker('getDate');
                                        var testEndDate = endDateTextBox.datetimepicker('getDate');
                                        if (testStartDate > testEndDate)
                                                endDateTextBox.datetimepicker('setDate', testStartDate);
                                }
                                else {
                                        endDateTextBox.val(dateText);
                                }
                        },
                        onSelect: function (selectedDateTime){
                                endDateTextBox.datetimepicker('option', 'minDate', startDateTextBox.datetimepicker('getDate') );
                        }
                    });
                    endDateTextBox.datetimepicker({
                        timeFormat: 'hh:mm tt',  
                        onClose: function(dateText, inst) {
                                if (startDateTextBox.val() != '') {
                                        var testStartDate = startDateTextBox.datetimepicker('getDate');
                                        var testEndDate = endDateTextBox.datetimepicker('getDate');
                                        if (testStartDate > testEndDate)
                                                startDateTextBox.datetimepicker('setDate', testEndDate);
                                }
                                else {
                                        startDateTextBox.val(dateText);
                                }
                        },
                        onSelect: function (selectedDateTime){
                                startDateTextBox.datetimepicker('option', 'maxDate', endDateTextBox.datetimepicker('getDate') );
                        }
                    }); 
                }
            })
            
        })
        
    </script>
    <script>
    
            var startDateTextBox = $('#startDates');
            var endDateTextBox = $('#endDates');
            
            startDateTextBox.datetimepicker({
                timeFormat: 'hh:mm tt',
                onClose: function(dateText, inst) {
                        if (endDateTextBox.val() != '') {
                                var testStartDate = startDateTextBox.datetimepicker('getDate');
                                var testEndDate = endDateTextBox.datetimepicker('getDate');
                                if (testStartDate > testEndDate)
                                        endDateTextBox.datetimepicker('setDate', testStartDate);
                        }
                        else {
                                endDateTextBox.val(dateText);
                        }
                },
                onSelect: function (selectedDateTime){
                        endDateTextBox.datetimepicker('option', 'minDate', startDateTextBox.datetimepicker('getDate') );
                }
            });
            endDateTextBox.datetimepicker({
                timeFormat: 'hh:mm tt',  
                onClose: function(dateText, inst) {
                        if (startDateTextBox.val() != '') {
                                var testStartDate = startDateTextBox.datetimepicker('getDate');
                                var testEndDate = endDateTextBox.datetimepicker('getDate');
                                if (testStartDate > testEndDate)
                                        startDateTextBox.datetimepicker('setDate', testEndDate);
                        }
                        else {
                                startDateTextBox.val(dateText);
                        }
                },
                onSelect: function (selectedDateTime){
                        startDateTextBox.datetimepicker('option', 'maxDate', endDateTextBox.datetimepicker('getDate') );
                }
            });    
    </script>
    <script>
        //for all day
        $(function(){
            MakeStartEndPicker1("#startDate", "#endDate");
        });
        function MakeStartEndPicker1(startElement, endElement)
        {
                $(startElement).datepicker({
                        minDate: 0, 
                        dateFormat: 'yy-mm-dd', 
                        
                });

                $(endElement).datepicker({
                        minDate: 0, 
                        dateFormat: 'yy-mm-dd', 
                        buttonImageOnly: true, showAnim: 'slideDown',
                        duration: 0 ,
                        beforeShow: function(input)
                        {
                        var date1 =  $(startElement).datepicker('getDate');
                        if(date1 != undefined) return { minDate: date1 };
                        }
                });
                
        }
        //for date and time
        function dateTime(startDateTextBox,endDateTextBox){
            
            startDateTextBox.datetimepicker({
                timeFormat: 'hh:mm tt',
                onClose: function(dateText, inst) {
                        if (endDateTextBox.val() != '') {
                                var testStartDate = startDateTextBox.datetimepicker('getDate');
                                var testEndDate = endDateTextBox.datetimepicker('getDate');
                                if (testStartDate > testEndDate)
                                        endDateTextBox.datetimepicker('setDate', testStartDate);
                        }
                        else {
                                endDateTextBox.val(dateText);
                        }
                },
                onSelect: function (selectedDateTime){
                        endDateTextBox.datetimepicker('option', 'minDate', startDateTextBox.datetimepicker('getDate') );
                }
            });
            endDateTextBox.datetimepicker({
                timeFormat: 'hh:mm tt',  
                onClose: function(dateText, inst) {
                        if (startDateTextBox.val() != '') {
                                var testStartDate = startDateTextBox.datetimepicker('getDate');
                                var testEndDate = endDateTextBox.datetimepicker('getDate');
                                if (testStartDate > testEndDate)
                                        startDateTextBox.datetimepicker('setDate', testEndDate);
                        }
                        else {
                                startDateTextBox.val(dateText);
                        }
                },
                onSelect: function (selectedDateTime){
                        startDateTextBox.datetimepicker('option', 'maxDate', endDateTextBox.datetimepicker('getDate') );
                }
            }); 
        }
    </script>
    <?php
}
?>